package com.peng.kslc;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A6ZstrConvert
 * https://ssg.leetcode-cn.com/problems/zigzag-conversion/
 *
 * @author lupeng
 * Created on 2022-07-24
 */
public class A6ZstrConvert {

    private static final Logger logger = LoggerFactory.getLogger(A6ZstrConvert.class);

    public static void main(String[] args) {
        System.out.println(new A6ZstrConvert().convert("PAYPALISHIRING", 4));
    }

    public String convert(String s, int numRows) {
        if (numRows == 1 || numRows >= s.length()) {
            return s;
        }
        List<List<Character>> listArray = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            listArray.add(new ArrayList<>());
        }
        int index = 0;
        for (int i = 0; i < s.length(); i++) {
            listArray.get(index).add(s.charAt(i));
            int t = numRows + numRows - 2;
            if ((i % t) < numRows - 1) {
                index++;
            } else {
                index--;
            }
        }
        StringBuilder builder = new StringBuilder();
        listArray.forEach(carr -> carr.forEach(c -> builder.append(c.charValue())));
        return builder.toString();
    }
}
